package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.constant.MessageConstant;
import com.itheima.controller.dto.CheckGroupDto;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.CheckGroupCheckItemMapper;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.SetMealCheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.CheckgroupCheckitem;
import com.itheima.pojo.SetmealCheckgroup;
import com.itheima.service.CheckGroupService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description 检查组服务实现类
 * @Author makun
 * @Date 2023/9/13 14:19
 * @Version 1.0
 */
@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {
    @Autowired
    private CheckGroupCheckItemMapper checkGroupCheckItemMapper;
    @Autowired
    private SetMealCheckGroupMapper setMealCheckGroupMapper;

    @Override
    public List<CheckGroup> getAll(String queryString) {
        // 查询条件
        LambdaQueryWrapper<CheckGroup> queryPageBeanLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(queryString)) {
            // 编码
            queryPageBeanLambdaQueryWrapper.like( CheckGroup::getCode, queryString);
            queryPageBeanLambdaQueryWrapper.or();
            // 名称
            queryPageBeanLambdaQueryWrapper.like( CheckGroup::getName, queryString);
            queryPageBeanLambdaQueryWrapper.or();
            // 助记码
            queryPageBeanLambdaQueryWrapper.like( CheckGroup::getHelpCode, queryString);
        }
        return baseMapper.selectList(queryPageBeanLambdaQueryWrapper);
    }

    @Override
    public Page<CheckGroup> findPage(QueryPageBean queryPageBean) {
        // 查询条件
        LambdaQueryWrapper<CheckGroup> queryPageBeanLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(queryPageBean.getQueryString())) {
            // 编码
            queryPageBeanLambdaQueryWrapper.like( CheckGroup::getCode, queryPageBean.getQueryString());
            queryPageBeanLambdaQueryWrapper.or();
            // 名称
            queryPageBeanLambdaQueryWrapper.like( CheckGroup::getName, queryPageBean.getQueryString());
            queryPageBeanLambdaQueryWrapper.or();
            // 助记码
            queryPageBeanLambdaQueryWrapper.like( CheckGroup::getHelpCode, queryPageBean.getQueryString());
        }

        // 分页条件
        Page<CheckGroup> queryPageBeanPage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 分页查询
        baseMapper.selectPage(queryPageBeanPage, queryPageBeanLambdaQueryWrapper);
        // 返回数据
        return queryPageBeanPage;
    }

    @Override
    public Result findById(Integer id) {
        // 根据检查组ID查询检查组基本信息
        CheckGroup checkGroup = baseMapper.selectById(id);
        boolean flag = !Objects.isNull(checkGroup);

        return new Result(flag, flag ? MessageConstant.QUERY_CHECKGROUP_SUCCESS : MessageConstant.QUERY_CHECKGROUP_FAIL, checkGroup);
    }

    @Override
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        // 根据检查组ID查询改组下所有的检查项
        List<CheckgroupCheckitem> checkgroupCheckitems = checkGroupCheckItemMapper.selectList(new LambdaQueryWrapper<CheckgroupCheckitem>().eq(CheckgroupCheckitem::getCheckgroupId, id));
        // 获取所有检查项的ID
        List<Integer> checkItemIds = checkgroupCheckitems.stream()
                .map(CheckgroupCheckitem::getCheckitemId)
                .collect(Collectors.toList());
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemIds);
    }

    @Override
    public Result edit(Integer[] checkItemIds, CheckGroup checkGroup) {
        // 将多表关系中有关该检查组的信息全部删除
        checkGroupCheckItemMapper.delete(new LambdaQueryWrapper<CheckgroupCheckitem>().eq(CheckgroupCheckitem::getCheckgroupId, checkGroup.getId()));
        // 更新检查组的基本信息
        baseMapper.updateById(checkGroup);
        // 更新多表关系
        for (Integer checkItemId : checkItemIds) {
            checkGroupCheckItemMapper.insert(new CheckgroupCheckitem(checkGroup.getId(), checkItemId));
        }

        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS, null);
    }

    @Override
    public Result add(Integer[] checkItemIds, CheckGroup checkGroup) {
        // 新增检查组的基本信息
        baseMapper.insert(checkGroup);
        // 新增检查组与检查项的多表关系
        for (Integer checkItemId : checkItemIds) {
            checkGroupCheckItemMapper.insert(new CheckgroupCheckitem(checkGroup.getId(), checkItemId));
        }

        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS, null);
    }

    @Override
    public Result delete(Integer id) {
        // 删除该检查组与检查表的关系
        checkGroupCheckItemMapper.delete(new LambdaQueryWrapper<CheckgroupCheckitem>().eq(CheckgroupCheckitem::getCheckgroupId, id));
        // 删除该检查组与套餐表的关系
        setMealCheckGroupMapper.delete(new LambdaQueryWrapper<SetmealCheckgroup>().eq(SetmealCheckgroup::getCheckgroupId, id));
        // 删除检查组
        boolean flag = baseMapper.deleteById(id) == 1;
        return new Result(flag, flag ? MessageConstant.DELETE_CHECKGROUP_SUCCESS : MessageConstant.DELETE_CHECKGROUP_FAIL, null);
    }

    @Override
    public Result findCheckGroupIdsBySetMealId(Integer id) {
        // 查询条件
        LambdaQueryWrapper<SetmealCheckgroup> checkGroupLambdaQueryWrapper = new LambdaQueryWrapper<>();
        checkGroupLambdaQueryWrapper.eq(SetmealCheckgroup::getSetmealId, id);
        List<Integer> checkGroupIds = setMealCheckGroupMapper.selectList(checkGroupLambdaQueryWrapper).stream().map(SetmealCheckgroup::getCheckgroupId)
                .collect(Collectors.toList());
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupIds);
    }

    @Override
    public boolean saveOne(CheckGroupDto checkGroupDto) {
        // 新增检查组的基本信息
        baseMapper.insert(checkGroupDto);
        // 新增检查组与检查项的多表关系
        for (Integer checkItemId : checkGroupDto.getCheckItemIds()) {
            checkGroupCheckItemMapper.insert(new CheckgroupCheckitem(checkGroupDto.getId(), checkItemId));
        }
        return true;
    }
}
